/*
 * UPSpringBody.java
 *
 * Created on 2010-06-15, 00:27:14
 */

package JET.UtilPanels;

import JET.Centers.ControlCenter;
import JET.Frames.DataModels.MaterialComboBoxModel;
import JET.Centers.Graphics.GUICenter;
import JET.MouseStuff.SpecialTasks.*;
import JET.MouseStuff.Tasks.*;
import JET.SA.SA_Selection;
import JET.SA.SA_SpringBody;
import JET.Scene;
import JET.StaticActions;
import JET.mygui.UtilTabsPane.UtilTabsPaneTab;
import JET.physics.Mass;
import JET.physics.bodies.SpringBody;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

/**
 *
 * @author 057Ry
 */
public class UPSpringBody extends javax.swing.JPanel implements UtilTabsPaneTab {

    static final ControlCenter CC = ControlCenter.getInstance();
    private Scene scene;

    /** Creates new form UPSpringBody */
    public UPSpringBody() {
        initComponents();
    }

    public void setScene(Scene s) {
        scene = s;
    }

    public GUICenter.UtilTabs getId() {
        return GUICenter.UtilTabs.SpringBody;
    }

    public boolean isScrollable() {
        return true;
    }

    public void setSpringBodyNameText(String text) {
        sbNameTextField.setText(text);
    }
    public void setSpringBodyMaterialIndex(int index) {
        sbMaterialComboBox.setSelectedIndex(index);
    }
    public void setSpringBodyShapeMatchingActivatedCheckBoxEnabled(boolean enabled) {
        sbSMActivateCheckBox.setEnabled(enabled);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sbNameLabel = new javax.swing.JLabel();
        sbNameTextField = new javax.swing.JTextField();
        sbMaterialLabel = new javax.swing.JLabel();
        sbMaterialComboBox = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        sbSMActivateCheckBox = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        sbNameLabel.setText("Name :");

        sbNameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                sbNameTextFieldKeyPressed(evt);
            }
        });

        sbMaterialLabel.setText("Material :");

        sbMaterialComboBox.setModel(new MaterialComboBoxModel());
        sbMaterialComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sbMaterialComboBoxActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Shape Matching"));

        sbSMActivateCheckBox.setText("activated");
        sbSMActivateCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sbSMActivateCheckBoxActionPerformed(evt);
            }
        });

        jButton1.setText("Select Masses");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton5.setText("Clear Masses");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
            .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
            .addComponent(sbSMActivateCheckBox, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(sbSMActivateCheckBox)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(sbMaterialLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(sbMaterialComboBox, 0, 82, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(sbNameLabel)
                        .addGap(18, 18, 18)
                        .addComponent(sbNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sbNameLabel)
                    .addComponent(sbNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sbMaterialLabel)
                    .addComponent(sbMaterialComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getAccessibleContext().setAccessibleName("Spring body properties");
    }// </editor-fold>//GEN-END:initComponents

    private void sbNameTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sbNameTextFieldKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            if (scene.EC.currentBody instanceof SpringBody) {
                SpringBody sb = (SpringBody) scene.EC.currentBody;
                SA_SpringBody.setSpringBodyName(sb, sbNameTextField.getText(), true);
            }
        }
}//GEN-LAST:event_sbNameTextFieldKeyPressed

    private void sbMaterialComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sbMaterialComboBoxActionPerformed
        if (scene!=null && scene.EC!=null && scene.EC.currentBody instanceof SpringBody) {
            SpringBody sb = (SpringBody)scene.EC.currentBody;
            int index = sbMaterialComboBox.getSelectedIndex();
            sb.setMaterial( index );
            CC.GUIC.materialsDialog.setSelectedCell(index, index);
        }
    }//GEN-LAST:event_sbMaterialComboBoxActionPerformed

    private void sbSMActivateCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sbSMActivateCheckBoxActionPerformed
        ArrayList<SpringBody> sbList = scene.EC.selectionGroup.getSpringBodies();
        ArrayList<Mass> mList;

        //If selection is empty, apply shape matching to actual spring body
        if(sbList.isEmpty() && scene.EC.currentBody instanceof SpringBody)
            if (scene.EC.currentBody != null) {
                SA_Selection.addSpringBodyToSelection((SpringBody) scene.EC.currentBody, scene, false);
                sbList.add((SpringBody) scene.EC.currentBody);
            } else
                return;

        for(SpringBody sb : sbList) {
            if(sbSMActivateCheckBox.isSelected()) {
                mList = scene.EC.selectionGroup.getSpringBodyMasses(sb);
                sb.buildShapeMatchingStruct(mList);
                sb.setMatchShape(true);
            } else
                sb.setMatchShape(false);
        }

        CC.GUIC.repaintAllSceneTabs();
}//GEN-LAST:event_sbSMActivateCheckBoxActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (scene.EC.currentBody instanceof SpringBody) {
            SpringBody sb = (SpringBody) scene.EC.currentBody;
            sb.setMatchShape(false);

            scene.EC.selectionGroup.clear();
            for(Mass m : sb.getShapeMatchingMasses())
                SA_Selection.addMassToSelection(m, scene, true);

            CC.MC.getMouseModeSequence().clear();
            //CC.MC.getMouseModeSequence().addTask( new HidePropertiesDialogTask() );
            CC.MC.getMouseModeSequence().addTask( new SelectMassesTask(scene) );
            CC.MC.getMouseModeSequence().addTask( new ShowPropertiesDialogTask() );
            CC.MC.getMouseModeSequence().addTask( new DoNothingTask() );
            CC.MC.getMouseModeSequence().begin();
            CC.GUIC.repaintAllSceneTabs();
        }
}//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JComboBox sbMaterialComboBox;
    private javax.swing.JLabel sbMaterialLabel;
    private javax.swing.JLabel sbNameLabel;
    private javax.swing.JTextField sbNameTextField;
    private javax.swing.JCheckBox sbSMActivateCheckBox;
    // End of variables declaration//GEN-END:variables

}
